#coding: utf-8

from typing import List

class Solution:
    # 双指针
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        # 首先将数组进行排序
        nums.sort()
        # 先固定一个位置，然后再来俩指针移动
        ans = [abs(1e10),0,0,0]
        for i in range(len(nums)):
            low = i+1
            high = len(nums)-1
            while low < high:
                three_sum = nums[i]+nums[low]+nums[high]        
                re = target - three_sum
                if abs(re) < abs(ans[0]):
                    ans = [abs(re),i,low,high]

                if re == 0:
                    ans = [0,i,low,high]
                    break
                elif re>0:
                    # 此时三个数的和太小
                    low = low + 1
                else:
                    high = high - 1

        return nums[ans[1]]+nums[ans[2]]+nums[ans[3]]
                
            
if __name__ == '__main__':
    solu = Solution()
    print(solu.threeSumClosest([-1,0,1,1,55],3))
